#include<Servo.h> // Using servo library to control ESC
#include<SoftwareSerial.h>
//新建一个softSerial对象，rx:6,tx:5

SoftwareSerial softSerial1(11,10);

Servo esc; //Creating a servo class with name as esc
int val; //Creating a variable val
void setup()
{
  esc.attach(9); //Specify the esc signal pin,Here as D9
  esc.writeMicroseconds(1000);// initialize the signal to 1000
  
  Serial.begin(9600);
  softSerial1.begin(9600);
  //监听软串口通信
  softSerial1.listen();


}
String device_A_String = "";
int i;
void loop()
{
   
   while(softSerial1.available()>0)
    {  
     device_A_String+=(char)softSerial1.read();
    }
   if(device_A_String != "")
   {
    //Serial.println(device_A_String); 
    //i = (device_A_String[0]-'0')*10 + (device_A_String[1]-'0');
    i = device_A_String.toInt();
    Serial.print("软件"); 
    Serial.println(i); 
    int sudu = map(i, 0,100,1000,2000);
    //Serial.println(sudu); 
    esc.writeMicroseconds(sudu);// using val as the signal to esc
   }
   else
   {
    val=analogRead(A0); // Read input from analog pin a0 and store in val
    val= map(val, 0,1023,1000,2000); // mapping val to minimum and maximum(Change if needed)
    int value = (val-1000)/7;
    Serial.print("硬件");
    //Serial.print("  else");
    
    Serial.println(value);
    softSerial1.println(value);
    esc.writeMicroseconds(val);// using val as the signal to esc
   }
   device_A_String = "";
  delay(1000);
}
